import Vue from 'vue'
import App from './App'

import uView from '@/uni_modules/uview-ui'
Vue.use(uView)


// import router from "./router";
import store from './store'
import schema from 'async-validator'
import dialog from './utils/dialog'
import cookie from '@/utils/store/cookie'
import cuCustom from '@/components/colorui/components/cu-custom.vue'

import { parseRoute, _router, parseQuery } from '@/utils'
import {wxOriginalId, VUE_APP_RESOURCES_URL, VUE_APP_API_URL } from '@/config'
Vue.component('cu-custom', cuCustom)
Vue.config.productionTip = false
Vue.config.devtools = process.env.NODE_ENV !== 'production'


import validCode from './components/p-validCode/validCode.vue'

Vue.component('validcode',validCode)
// 导入并挂载全局的分享方法
import share from '@/common/share.js'
Vue.mixin(share)
Vue.prototype.$validator = function(rule) {
  return new schema(rule)
}

Vue.config.productionTip = false
App.mpType = 'app'
Vue.prototype.$store = store
import business from '@/components/business/business.vue';
import trends from '@/components/trends/trends.vue';
import assistant from '@/components/assistant/assistant.vue';
const app = new Vue({
  ...App,
  store,
  components: {
  	App,
  	business,
  	trends,
  	assistant
  }
})

Object.defineProperty(Vue.prototype, '$yrouter', {
  get() {
    return _router
  },
})

Object.defineProperty(Vue.prototype, '$yroute', {
  get() {
    return this._route
  },
})

Vue.prototype.$VUE_APP_API_URL = VUE_APP_API_URL
Vue.prototype.$wxOriginalId = wxOriginalId
Vue.component('cu-custom', cuCustom)

let deviceType = ''
// #ifdef APP-PLUS
// App平台编译的代码
deviceType = 'app'
Vue.prototype.$platform = uni.getSystemInfoSync().platform
// #endif

// #ifdef MP-WEIXIN ||MP-ALIPAY
// 微信小程序编译的代码
deviceType = 'routine'
// #endif

// !!! ps  不建议在 template 中使用 $deviceType 去判断当前环境，很有可能出现 $deviceType 为 undefined 导致判断出错的问题，可以在 script 模块中正常使用
// 建议通过 store 去获取 $deviceType 可以保证 template 中取到的值有效
// import { mapState, mapMutations, mapActions } from 'vuex';
// computed: {
// 		...mapState(['$deviceType'])
// },

// #ifdef H5
// H5编译的代码

import { wechat, clearAuthStatus, oAuth, auth, toAuth, pay, openAddress, openShareAll, openShareAppMessage, openShareTimeline, wechatEvevt, ready, wxShowLocation } from '@/libs/wechat'

import { isWeixin } from '@/utils'
const CACHE_KEY = 'clear_0.0.1'

if (!cookie.has(CACHE_KEY)) {
  // cookie.clearAll()
  cookie.remove('userInfo')
  cookie.remove('login_status')
  cookie.remove('boxSystem')
  cookie.remove('paymentPassword')
  cookie.set(CACHE_KEY, 1)
}

var urlSpread = parseQuery()['spread']
if (urlSpread) {
  cookie.set('spread', urlSpread)
}

// #endif

// #ifdef H5
// H5编译的代码
// 判断是否是微信浏览器
async function init() {
  if (isWeixin()) {
    deviceType = 'weixin'
    let wechatInit = wechat()
    if (wechatInit) {
      await oAuth()
    }
  } else {
    deviceType = 'weixinh5'
  }
}
init()
// #endif

Vue.prototype.$deviceType = deviceType

Vue.mixin({
  onLoad() {
    const { $mp } = this.$root
    this._route = parseRoute($mp)
  },
  onShow() {
    _router.app = this
    _router.currentRoute = this._route
  },
  // 这里为了解决 .vue文件中 template 无法获取 VUE.prototype 绑定的变量
  computed: {
    $VUE_APP_RESOURCES_URL() {
      return VUE_APP_RESOURCES_URL
    },

  },
  
  methods: {
	  //返回
	  back(res) {
	  	this.showToast(res.msg)
	  	if(res.code==1){
	  		setTimeout(() => {
	  			uni.navigateBack()
	  		}, 1500)
	  		
	  	}
	  },
	  getCurrentDate() {
	      const now = new Date();
	      const year = now.getFullYear();
	      const month = String(now.getMonth() + 1).padStart(2, '0'); // 月份从0开始，需要+1
	      const day = String(now.getDate()).padStart(2, '0');
	       // return ``;
	      return `${year}-${month}-${day}`;
	  		  
	  },
  	//校验是不是身份证号
  	isValidChineseId(id) {
  	    // 18位身份证正则表达式
  	    const regex = /^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(10|11|12))((0[1-9])|([1-2][0-9])|30|31)\d{3}[\dXx]$/;
  	    return regex.test(id);
  	},
  	//校验是不是手机号
  	isValidPhoneNumber(phoneNumber) {
  		const regex = /^1[3-9]\d{9}$/;
  		return regex.test(phoneNumber);
  	},
  
  		//跳转
  		toUrl(url) {
  			uni.navigateTo({
  				url: url
  			})
  		},
  		//弹出框
  		showToast(title) {
  			uni.showToast({
  				title: title,
  				icon: 'none',
  				mask:true,
  				duration:1500,
  			});
  		},
  		//查看文件
  		downloadFileUrl(url,type=2) {
  			console.log(url)
  				if(type==1){
  					uni.previewImage({
  						urls:[url]
  					})
  				}
  			else{
  				// fileType: url.substring(url.lastIndexOf('.') + 1),
  				uni.downloadFile({
  					url: url,
  					success(res) {
  						if (res.statusCode === 200) {
  							uni.openDocument({
  								filePath: res.tempFilePath,
  								
  								showMenu: true
  							});
  						}
  					},
  					fail(res) {
  						console.log(res)
  						uni.showToast({
  							title: '文件下载失败',
  							icon: 'none'
  						});
  					}
  				});
  			}
  			
  		},
  	
  	},
})

store.commit('updateDevicetype', deviceType)

app.$mount()
